var courtNames = [];
var rates = [];
var totalNums = [];
var nums = [];
var threeNums = [];
var chartsindex = 0;//柱状图的总页数
var arrayIndex = [];//柱状图的数据
var nowIndex = 0;//柱状图当前的页数
var resultValue = [];//用于存放json值
$(function () {
    //查询的开始时间默认给今天
    $('#startTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $('#endTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    findTechnologyCourtRate();
    $("#left").on("click", goLeft);
    $("#right").on("click", goRight);
    $("#loading").hide();
    $(".container-fluid").show();
    /* $("#_search").click(function () {
     init();
     });*/

});


function init() {

    findTechnologyCourtRate();
}
function initLeftCharts() {
    loadLeftCharts(arrayIndex[0].nowRates, arrayIndex[0].nowTotals, arrayIndex[0].nowNums, arrayIndex[0].nowCourtnames);
    //nowIndex = 0;
}
function goLeft() {
    if (nowIndex > 0) {
        loadLeftCharts(arrayIndex[nowIndex - 1].nowRates, arrayIndex[nowIndex - 1].nowTotals, arrayIndex[nowIndex - 1].nowNums, arrayIndex[nowIndex - 1].nowCourtnames);
        nowIndex = nowIndex - 1;
    }

}

function goRight() {
    if (chartsindex > nowIndex + 1) {
        loadLeftCharts(arrayIndex[nowIndex + 1].nowRates, arrayIndex[nowIndex + 1].nowTotals, arrayIndex[nowIndex + 1].nowNums, arrayIndex[nowIndex + 1].nowCourtnames);
        nowIndex = nowIndex + 1;
    }
}

function findTechnologyCourtRate() {
    courtNames = [];
    rates = [];
    totalNums = [];
    nums = [];
    threeNums = [];
    chartsindex = 0;//柱状图的总页数
    arrayIndex = [];//柱状图的数据
    nowIndex = 0;//柱状图当前的页数
    $.ajax({
        url: r.base_path + "caseCount/getTechnologyCourtRate",
        type: "post",

        data: {
            courtUid: r.court_uid
        },
        dataType: "json",
        success: function (result) {
            result = result.data;
            result.sort(function compare(a, b) {
                return b.rate - a.rate;
            });
            resultValue = result;
            $.each(result, function (i, ele) {
                courtNames.push(ele.name);
                rates.push(ele.rate);
                totalNums.push(ele.totalNum);
                nums.push(ele.num);
                threeNums.push(ele.threeNum);
            });
            /* var arrIndex = 0;
             chartsindex = Math.ceil(rates.length/10);
             for(var m=0; m<chartsindex; m++){
             if(m!=chartsindex-1){
             var nowCourtnames = courtnames.slice(arrIndex,arrIndex+10);
             var nowRates = rates.slice(arrIndex,arrIndex+10);
             var nowTotals = totalNums.slice(arrIndex,arrIndex+10);
             var nowNums = nums.slice(arrIndex,arrIndex+10);
             arrIndex = arrIndex + 10;
             arrayIndex.push({
             "nowCourtnames":nowCourtnames,
             "nowRates":nowRates,
             "nowTotals":nowTotals,
             "nowNums":nowNums

             });
             }else{
             var nowCourtnames = courtnames.slice(arrIndex,courtnames.length);
             var nowRates = rates.slice(arrIndex,rates.length);
             var nowTotals = totalNums.slice(arrIndex,totalNums.length);
             var nowNums = nums.slice(arrIndex,nums.length);
             arrayIndex.push({
             "nowCourtnames":nowCourtnames,
             "nowRates":nowRates,
             "nowTotals":nowTotals,
             "nowNums":nowNums
             });
             }

             }*/
            //initLeftCharts();
            loadLeftCharts(rates, totalNums, nums, courtNames);
            loadTableCharts(result);
        }
    });

}

function loadLeftCharts(rates, totals, nums, list) {
    $("#pic").show();
    var myChart = echarts.init(document.getElementById('canvas'), "shine");
    var option = {
        title: {
            text: "科技法庭建设百分比",
            x: "left",
            y: "top",
            textStyle: {
                color: "#FFFFFF"
            }
        },
        legend: {
            data: ['法庭总数', '科技法庭数', '百分比'],
            textStyle: {
                color: "#FFFFFF"
            }
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        grid: { // 控制图的大小，调整下面这些值就可以，
            borderColor: "#1c4e93"
        },
        color: ["#d5b136"],
        /* tooltip: {
         show: true,
         formatter: "{b}<br/>{c}"
         },*/
        xAxis: {
            splitLine: {
                show: false
            },
            axisLabel: {
                textStyle: {
                    color: "#FFFFFF",
                    fontSize: 14
                },
                // rotate: -30,
                interval: 0
            },
            axisLine: {
                lineStyle: {
                    color: "#1c4e93"
                }
            },
            type: 'category',
            data: list
        }
        ,
        yAxis: [
            {
                splitLine: {
                    show: false
                },
                splitArea: {
                    show: true,
                    areaStyle: {
                        color: ["#103d80", "#18498b"]
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: "#1c4e93"
                    }
                },
                axisLabel: {
                    textStyle: {
                        color: "#FFFFFF"
                    }
                },
                type: 'value'
            }
        ]
        ,
        series: [
            {
                name: '百分比',
                type: 'line',
                data: rates,
                itemStyle: {
                    normal: {
                        color: 'red',
                        label: {
                            show: true,
                            position: 'inside',
                            textStyle: {
                                color: "yellow",
                                fontSize: 20
                            }
                        }
                    }
                }
            },
            {
                name: '法庭总数',
//                barWidth: '30%',
                barMaxWidth: 20,
                type: 'bar',
                itemStyle: {
                    normal: {
                        color: '#ff9224',
                        label: {
                            show: true,
                            position: 'top',
                            textStyle: {
                                color: "#d94600",
                                fontSize: 20
                            }
                        }
                    }
                },
                data: totals
            },
            {
                name: '科技法庭数',
//                barWidth: '30%',
                barMaxWidth: 20,
                type: 'bar',
                itemStyle: {
                    normal: {
                        color: 'yellow',
                        label: {
                            show: true,
                            position: 'top',
                            textStyle: {
                                color: "#d94600",
                                fontSize: 20
                            }
                        }
                    }
                },
                data: nums
            }
        ]
    };
    myChart.setOption(option);
    window.onresize = myChart.resize;
}

function loadTableCharts(result) {
//	$('#tablecontain').text("");
//	var tableEle = "<table id=\"table\"></table>";
//	$('#tablecontain').append(tableEle);
    $('#table').bootstrapTable({
        columns: [{
            field: 'Number',
            title: '序号',
            width: '5.3%',
            formatter: function (value, row, index) {
                return (index + 1) > 9 ? index + 1 : '0' + (index + 1);
            }
        }, {
            field: 'name',
            title: '法院'
        }, {
            field: 'totalNum',
            title: '法庭总数',
            sortable: true,
            sorter: function (a, b) {
                return a - b;
            }
        }, {
            field: 'num',
            sortable: true,
            title: '科技法庭数'
        }, {
            field: 'rate',
            sortable: true,
            title: '数字法庭建设率'
        }],
        data: result,
        sortName: 'uid',
        sortOrder: 'asc',
        sortStable: true,
        onClickRow: function (row) {

            if (row.courtUid == r.court_uid) {
                window.location.href = r.base_path + 'system_monitor/sys_court_status?courtUid=' + row.courtUid;
            } else if (row.length == 0) {
                window.location.href = r.base_path + 'system_monitor/sys_court_status?courtUid=' + row.courtUid;
            } else {
                base.change_court(row.courtUid);
            }
        },
        onSort: function (name, order) {
            if ("totalNum" == name) {
                orderByTotal(order);
            } else if ("num" == name) {
                orderByNum(order);
            } else if ("rate" == name) {
                orderByRate(order);
            }
        }
    });
    $('#table').bootstrapTable("load", result);
}

// $("#table").find("tbody").html("");
// $.each(result,function(i,ele){
// 	var tr =  "<tr><td>"+(i+1)+"</td><td class='court-hand' onclick='gotoCity(\""+ ele.id+"\")'>"+ele.name+"</td><td>"+ele.totalnum+"</td><td>"+ele.num+"</td><td>"+ele.rate+"</td><tr>";
// 	$("#table").find("tbody").append(tr);
// });
// $("#table").show();


function orderByTotal(order) {
    var courtNames = [];
    var rates = [];
    var totalNums = [];
    var nums = [];
    if ("asc" == order) {
        resultValue.sort(function compare(a, b) {
            return a.totalNum - b.totalNum;
        });
    } else {
        resultValue.sort(function compare(a, b) {
            return b.totalNum - a.totalNum;
        });
    }
    $.each(resultValue, function (i, ele) {
        courtNames.push(ele.name);
        rates.push(ele.rate);
        totalNums.push(ele.totalNum);
        nums.push(ele.num);
    });
    loadLeftCharts(rates, totalNums, nums, courtNames);
}

function orderByNum(order) {
    var courtNames = [];
    var rates = [];
    var totalNums = [];
    var nums = [];
    if ("asc" == order) {
        resultValue.sort(function compare(a, b) {
            return a.num - b.num;
        });
    } else {
        resultValue.sort(function compare(a, b) {
            return b.num - a.num;
        });
    }
    $.each(resultValue, function (i, ele) {
        courtNames.push(ele.name);
        rates.push(ele.rate);
        totalNums.push(ele.totalNum);
        nums.push(ele.num);
    });
    loadLeftCharts(rates, totalNums, nums, courtNames);
}

function orderByRate(order) {
    var courtNames = [];
    var rates = [];
    var totalNums = [];
    var nums = [];
    //var tdtotal = $("#tdrate").attr("sort");
    if ("asc" == order) {
        resultValue.sort(function compare(a, b) {
            return a.rate - b.rate;
        });
        //$("#tdrate").attr("sort","up");
    } else {
        resultValue.sort(function compare(a, b) {
            return b.rate - a.rate;
        });
        //$("#tdrate").attr("sort","down");
    }
    $.each(resultValue, function (i, ele) {
        courtNames.push(ele.name);
        rates.push(ele.rate);
        totalNums.push(ele.totalNum);
        nums.push(ele.num);
    });
    loadLeftCharts(rates, totalNums, nums, courtNames);
    //loadTableCharts(resultValue);
}
/*function loadTableCharts(result){
 $("#table").find("tbody").html("");
 $("#table").find("thead").html("");
 $('#table').bootstrapTable({
 columns: [{
 field: 'name',
 title: '法院名称'
 }, {
 field: 'totalnum',
 title: '法庭总数'
 }, {
 field: 'num',
 title: '科技法庭数'
 },{
 field: 'rate',
 title: '建设率'
 }],
 data: result
 });
 $("#table").attr( "data-sort-name","rate");
 $("#table").attr( "data-sort-order","desc");
 var tr = "<tr>";
 tr = tr +"<th data-field='name'  data-sortable='true'>Name</th>";
 tr = tr +"<th data-field='totalnum'  data-sortable='true'>Name</th>";
 tr = tr +"<th data-field='num'  data-sortable='true'>Name</th>";
 tr = tr +"<th data-field='rate'  data-sortable='true'>rate</th>";
 $("#table").find("thead").append(tr);
 $("#table").show();
 }*/
/*<table data-toggle="table"
 data-url="https://api.github.com/users/wenzhixin/repos?type=owner&sort=full_name&direction=asc&per_page=100&page=1"
 data-sort-name="stargazers_count"
 data-sort-order="desc">
 <thead>
 <tr>
 <th data-field="name"
 data-sortable="true">
 Name
 </th>
 <th data-field="stargazers_count"
 data-sortable="true">
 Stars
 </th>
 <th data-field="forks_count"
 data-sortable="true">
 Forks
 </th>
 <th data-field="description"
 data-sortable="true">
 Description
 </th>
 </tr>
 </thead>
 </table>*/